<?php 
session_start();
$admin_area=1;
$admin = 1;
$req_level=1;
if (!isset($_SESSION["admin"]))
{
	$host  = $_SERVER['HTTP_HOST'];
	$uri  = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
	$extra = 'login.php';
	header("Location: http://$host$uri/$extra");
	exit;
}
else 
{
	require("../includes/common_admin_inc.php");
	
	if ($set_level_permissions)
	{
		$lCurrentPage = $_SERVER["SCRIPT_NAME"]; 
		$lCurrentPage=split("/",$lCurrentPage);
		$lCurrentPage=end($lCurrentPage);
		
		function getPermLevel($aPage)
		{
			
			global $set_level_perm_level1,$set_level_perm_level2,$set_level_perm_level3;
			
			$lFileArray_level1=split(",",$set_level_perm_level1);
			if (in_array($aPage,$lFileArray_level1))
				return 1;
			$lFileArray_level2=split(",",$set_level_perm_level2);
			if (in_array($aPage,$lFileArray_level2))
				return 2;
			$lFileArray_level3=split(",",$set_level_perm_level3);
			if (in_array($aPage,$lFileArray_level3))
				return 3;
		}
		
		$req_level=getPermLevel($lCurrentPage);
		
		if ($_SESSION["admin_level"]>$req_level AND isset($req_level))
		{
			echo "You do not have enough permissions to access this.<br />";
			echo "You have access level " . $_SESSION["admin_level"] . ", but this page requires level " . $req_level . ".";
			
			echo "<p><a href='index.php'>Return to main admin area</a></p>";
			exit;
		}
	}
}

if (getParam("logout",""))
{
	session_destroy();
	redirect("login.php");
}

if (isset($lIsMod))
	$lAddPath="../../";

header("Content-type: text/csv");
header("Content-Disposition: attachment; filename='newsletter_destinatarios.csv'");	
	
ini_set("max_execution_time","1000");

$mercadoVial 	   = $_REQUEST["vial"];
$mercadoTransporte = $_REQUEST["trans"];
$mercadoAgricola   = $_REQUEST["agro"];
$mercadoGruas 	   = $_REQUEST["grua"];
$mercadoServicios  = $_REQUEST["serv"];

$arrayCondiciones = array();
if($mercadoVial)
	$arrayCondiciones[] = "mercado_vial = 1";
if($mercadoTransporte)
	$arrayCondiciones[] = "mercado_transporte = 1";
if($mercadoGruas)
	$arrayCondiciones[] = "mercado_autoelevadores = 1";
if($mercadoAgricola)
	$arrayCondiciones[] = "mercado_agricola = 1";
if($mercadoServicios)
	$arrayCondiciones[] = "servicios = 1";
	
$users = getUsersFromNewsletterTableWhere($arrayCondiciones,array( 0 => 'email asc'), 0, 0);
foreach ($users as $user){		    			
	echo $user['email']."\n";
}

